home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CUJ9206.ARJ / 1006020A < prev    next >
Text File  |  1992-06-02  |  2KB  |  51 lines

  1. /* free function */
  2. #include "xalloc.h"
  3.  
  4. void (free)(void *ptr)
  5.         {       /* free an allocated data object */
  6.         _Cell *q;
  7.  
  8.         if (ptr == NULL)
  9.                 return;
  10.         q = (_Cell *)((char *)ptr - CELL_OFF);
  11.         if (q->_Size & _MEMBND)
  12.                 return; /* bad pointer */
  13.         if (_Aldata._Head == NULL
  14.                 || q < _Aldata._Head)
  15.                 {       /* insert at head of list */
  16.                 q->_Next = _Aldata._Head;
  17.                 _Aldata._Head = q;
  18.                 }
  19.         else
  20.                 {       /* scan for insertion point */
  21.                 _Cell *qp;
  22.                 char *qpp;
  23.  
  24.                 for (qp = _Aldata._Head;
  25.                         qp->_Next && q < qp->_Next; )
  26.                         qp = qp->_Next;
  27.                 qpp = (char *)qp + CELL_OFF + qp->_Size;
  28.                 if ((char *)q < qpp)
  29.                         return; /* erroneous call */
  30.                 else if ((char *)q == qpp)
  31.                         {       /* merge qp and q */
  32.                         qp->_Size += CELL_OFF + q->_Size;
  33.                         q = qp;
  34.                         }
  35.                 else
  36.                         {       /* splice q after qp */
  37.                         q->_Next = qp->_Next;
  38.                         qp->_Next = q;
  39.                         }
  40.                 }
  41.         if (q->_Next &&
  42.                 (char *)q + CELL_OFF + q->_Size == (char *)q->_Next)
  43.                 {       /* merge q and q->_Next */
  44.                 q->_Size += CELL_OFF + q->_Next->_Size;
  45.                 q->_Next = q->_Next->_Next;
  46.                 }
  47.         _Aldata._Plast = &q->_Next;    /* resume scan after freed */
  48.         }
  49.  
  50.  
  51.